<?php
namespace app\model;



class Fake {

    static function update($id,$data) {
        if (empty($id)) {
            return false;
        }
        db_update (t_fake,$data, array('id'=>$id));
        return true;
    }

    static function save($data){
        global $_G;
        $insertid = db_insert(t_fake, $data);
        $id = $insertid;
        return $id;
    }


    static function getById($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_fake) . " where id = :id";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }

    static function getLimitById($themeid,$limit=70) {
        if (empty($themeid)) {
            return false;
        }
        $sql = "SELECT * FROM ".tablename(t_fake)." as f
					WHERE themeid = '{$themeid}' and groupid = 0 and status =1 order by id desc limit ".$limit;
        $fakelist = db_select($sql);
        return $fakelist;
    }


    static function setCheckById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_fake, array("hascheck"=>1), array ('id' => $id) );
    }

    static function setHasNoticeById($id,$hasnotice = 1) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_fake, array("hasnotice"=>$hasnotice), array ('id' => $id) );
    }

    static function setCostAndLeftFeeById($costfee,$leftfee,$sendfee=0,$id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_fake, array("costfee"=>$costfee,"leftfee"=>$leftfee,"sendfee"=>$sendfee), array ('id' => $id) );
    }

    static function setIsDistributorById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_fake, array("isdistributor"=>1), array ('id' => $id) );
    }

    static function setEndDistributorById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_fake, array("isdistributor"=>2), array ('id' => $id) );
    }

    static function deleteById($id){
        if (empty($id)) {
            return false;
        }
        db_delete ( t_fake, array ('id' => $id) );
        return true;
    }


    static function getListByCondition($condition,$pageNumber,$pageSize){
        $sql = "SELECT master.* FROM " . tablename ( t_fake ) . " as master 
						WHERE   {$condition} ORDER BY master.id desc
									LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $list = db_select ( $sql );
        return $list;

    }

    static function getAttendNumByThemeIdAndStatus($themeid,$status = 1){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fake ) . " as master
					WHERE  themeid = :themeid and status = :status ORDER BY master.id", array(":themeid"=>$themeid,":status"=>$status) );
        return intval($num);
    }

    static function getAttendNumByThemeId($themeid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fake ) . " as master
					WHERE  themeid = :themeid and status > 0 ORDER BY master.id", array(":themeid"=>$themeid) );
        return intval($num);
    }

    static function getFirstAttendByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_fake ) . " as master 
						WHERE   themeid = :themeid and status = 1 ORDER BY master.id asc LIMIT 1";
        $list = db_find ($sql , array(":themeid"=>$themeid));
        return $list;
    }

    static function setFirstAttendOut($orderid){
        $list = db_update (t_fake,array('status'=>2) , array("id"=>$orderid,"status"=>1));
        return $list;
    }

    static function getAttendListByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_fake ) . " as master 
						WHERE   themeid = :themeid and status > 0 ORDER BY master.id desc LIMIT 16";
        $list = db_select ( $sql , array(":themeid"=>$themeid));
        return $list;

    }


    static function getNumByThemeIdAndStatus($themeid,$status=0){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fake ) . " as master
					WHERE  themeid = :themeid and status = :status ORDER BY master.id", array(":themeid"=>$themeid,":status"=>$status) );
        return intval($num);
    }

    static function getOutListByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_fake ) . " as master 
						WHERE   themeid = :themeid and status = 2 ORDER BY master.id LIMIT 20";
        $list = db_select ( $sql , array(":themeid"=>$themeid));
        return $list;

    }

    static function getListByThemeId($themeid){
        global $_G, $_P;
        // $cachekey = 'fake'.$_G['mpid'].MODULE_NAME.$themeid;
        // $settings = cache_load($cachekey);
        // if(empty($settings)){
            $sql = "SELECT id,nickname,avatar FROM ".tablename(t_fake)." as f
				WHERE themeid =:themeid and status != 0 order by id desc limit 140";
            $settings = db_select ( $sql , array(":themeid"=>$themeid));
        //     if(!empty($settings)){
        //         cache_write($cachekey, $settings);
        //     }
        // }
        return $settings;

    }

    static function getNumByOpenid($openid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fake ) . " as master
					WHERE  openid = :openid ORDER BY master.id", array(":openid"=>$openid) );
        return intval($num);
    }

    static function getNumByCondition($condition){
        return db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fake ) . " as master
					WHERE  {$condition} ORDER BY master.id" );

    }




    static function getByIdAndShopid($id,$shopid) {
        if (empty($id) || empty($shopid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_fake) . " where id = :id and shopid = :shopid";
        $row = db_find($sql, array(":id"=>$id,":shopid"=>$shopid));
        return $row;
    }

    static function getByThemeIdAndOpenid($themeid,$openid) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_fake) . " where themeid = :themeid and openid = :openid";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getByIdAndOpenid($id,$openid) {
        if (empty($id) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_fake) . " where id = :id and openid = :openid";
        $row = db_find($sql, array(":id"=>$id,":openid"=>$openid));
        return $row;
    }


    static function getFixListByOpenidOrMobile($openid,$mobile,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_fake) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.status >= 0 and r.type = 'repair' and r.uniacid = :uniacid and r.openid = :openid";
        $p = array(":uniacid"=>$uniacid,":openid"=>$openid);
        if(!empty($mobile)){
            $sql.=" or r.regmobile = :mobile";
            $p[":mobile"] = $mobile;
        }
        $sql .= " order by r.id desc limit 50";

        $list = db_select($sql,$p );
        return $list;

    }


    static function getUnNoticeList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_fake) . " as r
                        where r.uniacid = :uniacid and r.status = 0 and r.hasnotice = 0";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getCheckList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_fake) . " as r
                        where r.uniacid = :uniacid and r.status >= 1 and r.fee > 0 and r.hascheck = 0 order by r.id desc";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getDistributorList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_fake) . " as r
                        where r.uniacid = :uniacid and r.status >= 1 and r.fee > 0 and r.hascheck = 1 and r.isdistributor = 1 order by r.id desc";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getListByShopid($shopid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_fake) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getIsOpenListByShopid($shopid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_fake) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.isopen = 1
                                and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getListByShopidAndWorker($shopid,$wopenid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_fake) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.wopenid =:wopenid and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid,":wopenid"=>$wopenid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getListByOpenidOrMobile($openid,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img,s.title as shop,s.mobile as kfdh FROM " . tablename(t_fake) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        left join " . tablename(t_shop) . " as s on r.shopid = s.id
                        where r.status >= 0 and r.uniacid = :uniacid and r.openid = :openid order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":openid"=>$openid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getListByMobile($mobile,$uniacid){
        if (empty($mobile)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_fake) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.status >= 0 and r.uniacid = :uniacid and r.regmobile = :regmobile order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":regmobile"=>$mobile);
        $list = db_select($sql,$p );
        return $list;

    }


    static function getCancelByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $p = array(":openid"=>$openid,":uniacid"=>$uniacid);
        $p[':addtime'] = TIMESTAMP - 7*24*60*60;
        $hascancel = db_fetchcolumn ( "SELECT COUNT(*) FROM " . tablename (t_fake) . "
								WHERE uniacid = :uniacid and status = -1 and openid =:openid and addtime > :addtime",$p);
        return $hascancel;
    }

    static function getUndealFixByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_fake) . " 
                    where uniacid = :uniacid and type = 'repair' and status = 0 and openid =:openid limit 1";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getUndealRecycleByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_fake) . " 
                    where uniacid = :uniacid and type = 'recycle' and status = 0 and openid =:openid limit 1";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getTotalByStatus($status,$uniacid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_fake ) . " WHERE uniacid = :uniacid and status = :status";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getTotalByStatusByShopid($status,$uniacid,$shopid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid,":shopid"=>$shopid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_fake ) . " WHERE uniacid = :uniacid and status = :status and shopid =:shopid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getTotalByStatusByWorker($status,$uniacid,$shopid,$openid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid,":shopid"=>$shopid,":openid"=>$openid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_fake ) . " WHERE uniacid = :uniacid and status = :status and shopid =:shopid and wopenid = :openid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }


    static function getTotalByShopid($shopid){
        if(empty($shopid)){
            return false;
        }
        $p = array(":shopid"=>$shopid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_fake ) . " WHERE shopid = :shopid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getByUId($uid) {
        if (empty($uid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename("mc_members") . " where uid = :uid";
        $row = db_find($sql, array(":uid"=>$uid));
        return $row;
    }
    static function getByOpenid($openid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':uniacid'] = $_G['mpid'];
        $params[':openid'] = $openid;
        $fans = db_find("SELECT * FROM ".tablename(t_worker)." WHERE uniacid= :uniacid and openid = :openid",$params);
        return $fans;
    }

    static function getShopWorkerByOpenid($openid,$shopid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':uniacid'] = $_G['mpid'];
        $params[':openid'] = $openid;
        $params[':shopid'] = $shopid;
        $row = db_find("SELECT * FROM ".tablename(t_worker)." WHERE shopid = :shopid and uniacid= :uniacid and openid = :openid",$params);
        return $row;
    }



}
